package cn.Controller;

import cn.Service.HeaderService;
import cn.Service.IndexService;
import cn.entity.Product;
import cn.entity.Product_copy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class HeaderController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private HeaderService headerService;
    @GetMapping("/findAllProductByCid")
    public String findAlByCid(Integer cid, Model model,@RequestParam(defaultValue = "1",value = "PageNo") Integer PageNo ){
        Integer PageSize=8;
        PageHelper.startPage(PageNo,PageSize);
        List<Product> allProductByCid = headerService.findAllProductByCid(cid);
        PageInfo<Product> productPageInfo=new PageInfo<Product>(allProductByCid);
        List<Product_copy> product_copies = indexService.FindAllProduct_copy();
        model.addAttribute("IMGS",product_copies);
        model.addAttribute("PAGEPRODUCT",productPageInfo);
        return "product_list";
    }
    @GetMapping("/FindAllProductByPname")
    public String FindAllProductByPname(Model model, String key, @RequestParam(defaultValue = "1",value = "PageNo") Integer PageNo){
            Integer PageSize=8;
            PageHelper.startPage(PageNo,PageSize);
            List<Product> products = headerService.FindAllProductByPname(key);
            PageInfo<Product> productPageInfo=new PageInfo<Product>(products);
            model.addAttribute("KEY",key);
            model.addAttribute("PRODUCTS",productPageInfo);
            return "search";
    }
}
